<?php
Box::$description["Text"] = "An area of text";
/**
*Implements simple text boxes.
* Format text according to BBCdore tags.
*The following BBCode tags are supported:
*- [b]text[/b] - Bold text.
*- [i]text[/i] - Italic text.
*- [u]text[/u] - Underlined text (using CSS text-decoration).
*- [s]text[/s] - Strike-trough text (using CSS text-decoration).
*- [code]text[/code] - print code (using a div with class "code").
*- [quote]text[/quote] - print a quotation (using p with class "quote").
*- [quote=name]text[/quote] - print a named quotation (using p with class "quote" and span with class "quote").
*- [size=Y]text[/size] - Set font-size. Y is expressed in points (pt). Uses a styled span
*- [color=F]text[/color] - Set tetx color. Uses span with styles.
*- [ul]xxx[/ul] - Unnumbered list.
*- [ol]xxx[/ol] - Numbered list.
*- [li]text[/li] - List item.
*- [p]text[/p] - Paragraph.
*- [center]ytext[/center] - Center text. Uses p with style.
*- [url]url[/url] - make a link
*- [url=xxx]text[/url] - make a nicer link
*- [img]xxx[/img] - Show an image. NOTE: images are only read from storage area.
*
*The following extra tags are supported:
*- [align=center|left|right]text[/align] - Align text. Uses p with style.
*- [color=F,B]text[/color] - Set text foreground and background color. Uses span with style.
*- [list]xxx[/list] - Backward compatibility. Same as [ul].
*- [h1-6]text[/h1-6] - Headers (h1 trough h6). Uses h1-h6 tags.
*- [hide]text[/hide] - Hide something. Useful to put some text in place to show at later time. Code is not printed.
*@package Boxes
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*/
class Box_Text extends Box
{
	static private $valid_codes = array(
			'/(\[b\])(.+?)(\[\/b\])/', # [b][/b] => <b></b>
			'/(\[i\])(.+?)(\[\/i\])/', # [i][/i] => <i></i>
			'/(\[u\])(.+?)(\[\/u\])/', # [u][/u] => <span style=text-decoration:underline;"></span>
			'/(\[s\])(.+?)(\[\/s\])/', # [s][/s] => <span style=text-decoration:line-through;"></span>
			'/(\[code\])(.+?)(\[\/code\])/', # [code][/code] => <div class="code"></div>
			'/(\[quote\])(.+?)(\[\/quote\])/', # [quote][/quote] => <p class="quote"></p>
			'/(\[quote=)(.+?)(\])(.+?)(\[\/quote\])/', #[quote=name][/quote] => <p class="quote">name: text</p>
			'/(\[size=)(.+?)(\])(.+?)(\[\/size\])/', #[size=s][/size] => <span style=font-size:spt;"></span>
			'/(\[color=)([^,\]]+?)(,)(.+?)(\])(.+?)(\[\/color\])/', #[color=F,B][/color] => <span style=color:F;background:B"></span>
			'/(\[color=)(.+?)(\])(.+?)(\[\/color\])/', #[color=c][/color] => <span style=color:c;"></span>
			'/(\[list\])(.+?)(\[\/list\])/', #[list][/list] => <ul></ul>
			'/(\[ul\])(.+?)(\[\/ul\])/', #[ul][/ul] => <ul></ul>
			'/(\[ol\])(.+?)(\[\/ol\])/', #[ol][/ol] => <ol></ol>
			'/(\[li\])(.+?)(\[\/li\])/', #[li][/li] => <li></li>
			'/(\[p\])(.*?)(\[\/p\])/', # [p][/p] => <p></p>
			'/(\[align=)(.+?)(\])(.+?)(\[\/align\])/', #[align=what][/align] => <p style="text-align:what></p>
			'/(\[center\])(.+?)(\[\/center\])/', #[center][/center] => <p style="text-align:center></p>
			'/(\[h)([123456])(\])(.+?)(\[\/h\\2\])/', # [h1-6][/h1-6] => <i></i>
			'/(\[url\])(.*?)(\[\/url\])/',
			'/(\[url=)(.+?)(\])(.*?)(\[\/url\])/',
			'/(\[extli?nk[=;])(.*?)(\])(.*?)(\[\/extli?nk\])/',
			'/(\[hide\])(.*?)(\[\/hide\])/',
						);
	static private $valid_replace = array (
			'<b>\\2</b>',
			'<i>\\2</i>',
			'<span style="text-decoration:underline;">\\2</span>',
			'<span style="text-decoration:line-through;">\\2</span>',
			'<div class="code">\\2</div>',
			'<p class="quote"><i>\\2</i></p>',
			'<p class="quote"><span class="quote">\\2:</span><i>\\4</i></p>',
			'<span style="font-size:\\2pt;">\\4</span>',
			'<span style="color:\\2;background:\\4;">\\6</span>',
			'<span style="color:\\2;">\\4</span>',
			'<ul>\\2</ul>',
			'<ul>\\2</ul>',
			'<ol>\\2</ol>',
			'<li>\\2</li>',
			'<p>\\2</p>',
			'<p style="text-align: \\2;">\\4</p>',
			'<p style="text-align: center;">\\2</p>',
			'<h\\2>\\4</h\\2>',
			'<a href="\\2">\\2</a>',
			'<a href="\\2">\\4</a>',
			'<a href="\\2">\\4</a>',
			'',
							);

	private function bbcode($code)
	{
		# We need to escape all newlines first or preg_replace will not work properly!
		$code = str_replace( array("\r","\n"), array("|*&","%|&"), $code );
		# Replace all basic bbcode tags...
		$code = preg_replace( Box_Text::$valid_codes, Box_Text::$valid_replace, $code );
		# Put back newlines...
		$code = str_replace( array("|*&","%|&"),array("\r","\n"), $code );
		return $code;
	}

	public function __construct( $title )
	{
		parent::__construct( $title, "popup", 'string:{$data.text}' );
	}
	protected function PostInitialize()
	{
		$this->SetStorage(array("text" => "*not_defined_yet!*" ) );
		$this->AddEditTab( "box:Text/BoxTextEdit",
							I18N("Edit text..."),
							I18N("Save text"),
							"SaveText" );
	}

	protected function ProcessData( $data )
	{
		$data["data"]["unmodified_text"] = $data["data"]["text"];
		$data["data"]["text"] = $this->bbcode( $data["data"]["text"] );
		return $data;
	}

	protected function SaveText( $request )
	{
		$text = Core::PostVar("text", $this->Get("text"), "string" );
		$this->Set("text",$text ) && $request->SetReload(true);
		echo I18N("Text saved");
	}
}
?>